package com.xinz.serialization;

public abstract class Serialer {

	int index;
	
	boolean base;
	
	public void setIndex(boolean base, int index) {
		this.index = index;
		this.base = base;
	}
	
	public int getIndex() {
		return index;
	}
	
	public boolean isBase() {
		return base;
	}
	
	public abstract void read(Accessor accessor, BeanInputStream in) throws Exception;
	
	public abstract void write(Accessor accessor, BeanOutputStream out) throws Exception;
	
	public abstract Class getType();
	
}

class objectArraySerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
//		accessor.setInt(in.readInt());
		int size = in.readShort();
		if(size==-1){
			accessor.set(null);
			return;
		}
		Object array[] = new Object[size];
		ObjectAccessor oa = new ObjectAccessor();
		for(int i=0;i<size;i++){
			in.readAccessor(oa);
			array[i] = oa.get();
		}
		accessor.set(array);
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		Object[] array = (Object[]) accessor.get();
		if(array==null){
			out.writeShort(-1);
		}
		ObjectAccessor oa = new ObjectAccessor();
		out.writeShort(array.length);
		for(int i=0;i<array.length;i++){
			oa.set(array[i]);
			out.writeAccessor(oa);
		}
	}
	@Override
	public Class getType() {
		return Object[].class;
	}
}
class intSerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
		accessor.setInt(in.readInt());
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		out.writeInt(accessor.getInt());
	}
	@Override
	public Class getType() {
		return int.class;
	}
}
class floatSerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
		accessor.setFloat(in.readFloat());
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		out.writeFloat(accessor.getFloat());
	}
	@Override
	public Class getType() {
		return float.class;
	}
}
class shortSerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
		accessor.setShort(in.readShort());
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		out.writeShort(accessor.getShort());
	}
	@Override
	public Class getType() {
		return short.class;
	}
}
class booleanSerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
		accessor.setBoolean(in.readBoolean());
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		out.writeBoolean(accessor.getBoolean());
	}
	@Override
	public Class getType() {
		return boolean.class;
	}
}

class longSerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
		accessor.setLong(in.readLong());
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		out.writeLong(accessor.getLong());
	}
	@Override
	public Class getType() {
		return long.class;
	}
}

class doubleSerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
		accessor.setDouble(in.readDouble());
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		out.writeDouble(accessor.getDouble());
	}
	@Override
	public Class getType() {
		return double.class;
	}
}

class charSerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
		accessor.setChar(in.readChar());
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		out.writeChar(accessor.getChar());
	}
	@Override
	public Class getType() {
		return char.class;
	}
}
class byteSerialer extends Serialer{
	@Override
	public void read(Accessor accessor, BeanInputStream in)
			throws Exception {
		accessor.setByte(in.readByte());
	}
	@Override
	public void write(Accessor accessor, BeanOutputStream out)
			throws Exception {
		out.writeByte(accessor.getByte());
	}
	@Override
	public Class getType() {
		return byte.class;
	}
}

